package com.mk.settlement.common;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FeeUtil {
    
    private static Logger log = LoggerFactory.getLogger(FeeUtil.class);

    public static BigDecimal cal(BigDecimal p, int ordinal) {
        
        LadderPrice l =  LadderPrice.values()[ordinal];
        
        if(p.compareTo(l.end.subtract(l.begin)) <= 0 || ordinal == LadderPrice.values().length -1 ) {
            
            return p.multiply(l.proportion).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        
        return l.end.subtract(l.begin).multiply(l.proportion).add(cal(p.subtract(l.end.subtract(l.begin)),ordinal+1)).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    enum LadderPrice {

        STAGE1(new BigDecimal(0), new BigDecimal(5000), new BigDecimal(1.0)), STAGE2(new BigDecimal(5000),new BigDecimal(10000), new BigDecimal(0.95)),
        STAGE3(new BigDecimal(10000), new BigDecimal(20000), new BigDecimal(0.9)), STAGE4(new BigDecimal(20000),new BigDecimal(40000),new BigDecimal(0.85)),
        STAGE5(new BigDecimal(40000),new BigDecimal(100000), new BigDecimal(0.8));
        

        private BigDecimal begin;
        private BigDecimal end;
        private BigDecimal proportion;
        private LadderPrice(BigDecimal begin, BigDecimal end, BigDecimal proportion) {
            this.begin = begin;
            this.end = end;
            this.proportion = proportion;
        }

    }
    
}
